<?php
/**
 * house controller
 */
namespace App\Http\Controllers;

use App\Service\HouseRecoService;
use App\Validation\ListValidation;
use App\Providers\UrlServiceProvider;
use App\Service\ConfigsService;

class HouseRecoController extends BaseController
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const COLLECT_TRUE = 1;		//收藏标识：是
	const COLLECT_FALSE = 0;	//收藏标识：否
	const RECO_TRUE  = 1;		//获取首页列表标记
	const RECO_FALSE = 0;		//获取首页列表标记

	/**
	 * [indexAction 房源列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$arr = new \stdClass;
		$arr->name = "house_tags";
		$tags_obj = ConfigsService::getVal($arr);

		$this->getUser(0);
		$raw = $this->validation(new ListValidation, $this->request->getJsonRawBody());
		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;
		$raw->city_id = $this->city_id;

		$raw->is_display = self::DISPLAY_TRUE;
		$data  = HouseRecoService::getList($raw);

		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$tag_arr = explode(",", $value->house_tags);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}

			switch ($value->house_type) {
				case 'xinfang':
					$url = $this->config->urls->house;
					break;
				case 'ershoufang':
					$url = $this->config->urls->house_seco;
					break;
				case 'zufang':
					$url = $this->config->urls->house_rent;
					break;
				default:
					$url = $this->config->urls->house;
					break;
			}

			$d[$key] = $value;
			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];

			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			
			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";

			$d[$key]->create_time = $this->getShowTime($value->create_time);
			
			$d[$key]->layout = $value->layout ? $value->layout : "户型暂无";
			$d[$key]->area = $value->area ? $value->area . "㎡" : "面积未知";
			$d[$key]->house_orien = $value->house_orien ? $value->house_orien : "朝向不明";

			$d[$key]->price = $value->price ? $value->price . $value->unit : "单价未知";
			$d[$key]->total_price = $value->total_price ? $value->total_price . "万" : "总价未知";
			// $d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			// $d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;

			$is_praise = $value->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $url . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $url . $value->id;

			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
			$d[$key]->is_collect = intval($value->is_collect) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
		}

		// $count = HouseRecoService::getList($raw, TRUE);
		$count = HouseRecoService::count();
		$assign = [
			'total' => $count,
			'list'  => $d
		];

		return $this->echoJson($assign);
	}

	/**
	 * [detailAction 房源详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser(0);

		$id = $this->dispatcher->getParam('id');
		if (!$id)
			return $this->echoJson();

		if (isset($this->user)){
			$data = HouseRecoService::getInfo($id, $this->user->member_id);
		}else{
			$data = HouseRecoService::getInfo($id);
		}
		
		$arr = new \stdClass();
		if ($data){
			$tag_obj = new \stdClass;
			$tag_obj->name = "house_tags";
			$tags_obj = ConfigsService::getVal($tag_obj);
			$tag_arr = explode(",", $data->house_tags);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}
			foreach ($data as $key => $value) {
				$arr->$key = $value;
			}
			$arr->go_url = $this->config->gourl . $this->config->urls->house . $data->id;
			//判断缩略图是否有图且url是否完整
			$arr->thumb = $data->thumb ? $this->getFullUrl($data->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			if ($data->pics){
				$arr->pics = explode(',', $data->pics);
			}else{
				$arr->pics = [];
			}
			$arr->tags        = $data->tags ? $data->tags : "";
			$arr->house_tags  = $data->house_tags ? $tag_ar : [];
			$arr->comment = $data->comment > 10000 ? floor($data->comment / 10000) . '万+' : $data->comment;
			$arr->visit = $data->visit > 10000 ? floor($data->visit / 10000) . '万+' : $data->visit;
			$arr->sources = $data->sources ? $data->sources : "觅窝";
		}

		return $this->echoJson($arr);
	}
}